﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using Betop.Lib.Core.Base;
using Betop.Lib.Data;
using Betop.Lib.Utilities;
using Betop.Lib.Core.User;
using Betop.Lib.Core.Tree;


namespace Betop.Lib.Core.Right
{
    class SystemRightDAL:BaseDAL,ISystemRightDAL
    {
        private int _ownerType;

        #region ISystemRightDAL 成员

        public int OwnerType
        {
            set 
            {
                _ownerType = value;
            }
        }

        public SystemRightDAL(int ownerType)
        {
            this._ownerType = ownerType;
        }

        public void SaveRight(int ownerID, int rightID)
        {
            List<int> lstRightIDs = new List<int>(1);
            lstRightIDs.Add(rightID);
            SaveRight(ownerID, lstRightIDs);
        }

        public void SaveRight(int ownerID, List<int> lstRightID)
        {
            string[] strSqls = new string[lstRightID.Count + 1];
            strSqls[0] = "delete from SystemRight where OwnerID=" + ownerID + " and OwnerType=" + _ownerType;
            for (int i = 0; i < lstRightID.Count; i++)
            {
                strSqls[i + 1] = "insert into SystemRight(RightID,OwnerID,OwnerType) values(" + lstRightID[i] + "," + ownerID + "," + _ownerType + ")";
            }
            dbOper.RunSql(strSqls);
        }

        public void SaveRight(List<int> lstOwnerID, int rightID)
        {
            string[] strSqls = new string[lstOwnerID.Count + 1];
            strSqls[0] = "delete from SystemRight where RightID=" + rightID + " and OwnerType=" + _ownerType;
            for (int i = 0; i < lstOwnerID.Count; i++)
            {
                strSqls[i + 1] = "insert into SystemRight(RightID,OwnerID,OwnerType) values(" + rightID + "," + lstOwnerID[i] + "," + _ownerType + ")";
            }
            dbOper.RunSql(strSqls);
        }

        public List<int> GetRightIDs(int ownerID)
        {
            string strSql = "select RightID from SystemRight where OwnerID=" + ownerID + " and OwnerType=" + _ownerType;
            DataTable dt = dbOper.Query(strSql);
            List<int> lstRightIDs = ConvertUtil.ConvertToList(dt);
            return lstRightIDs;
        }

        public List<int> GetOwnerIDs(int rightID)
        {
            string strSql = "select OwnerID from SystemRight where RightID=" + rightID + " and OwnerType=" + _ownerType;
            DataTable dt = dbOper.Query(strSql);
            List<int> lstOwnerIDs = ConvertUtil.ConvertToList(dt);
            return lstOwnerIDs;
        }

        public bool HaveRight(int ownerID, int rightID)
        {
            string strSql = "select count(*) from SystemRight where RightID=" + rightID + " and OwnerID=" + ownerID + " and OwnerType=" + _ownerType;

            if (_ownerType == (int)RightOwnerType.User)//对于是用户ID的，必须把它所有的部门和所有角色的ID拥有的权限合并起来
            {
                UserDAL user = new UserDAL();

                List<int> lstDeptIDs = UserDAL.GetDeptIDs(ownerID);
                lstDeptIDs = (new TreeDAL<TreeInfo>("Dept")).GetParentIDs(lstDeptIDs);
                string strDeptIDs = ConvertUtil.ConvertToDbStr(lstDeptIDs);


                List<int> lstRoleIDs = user.GetRoleIDs(ownerID);
                string strRoleIDs = ConvertUtil.ConvertToDbStr(lstRoleIDs);

                strSql = "select count(*) from SystemRight where RightID = " + rightID + " and ((OwnerID = " + ownerID + " and OwnerType = " + _ownerType + ") or " +
                                                                            "(OwnerID in (" + strDeptIDs + ") and OwnerType = " + (int)RightOwnerType.Dept + ") or " +
                                                                            "(OwnerID in (" + strRoleIDs + ") and OwnerType = " + (int)RightOwnerType.Role + "))";
              
            }

            int count = dbOper.GetScalar<int>(strSql);
            bool flag = (count == 0) ? false : true;
            return flag;
        }

        #endregion

        internal static string GetRightSet()
        {
            DBHelper db = DBHelper.Instance();
            string sql = "select RightIDs from SystemRightSet ";
            DataTable dt = db.Query(sql);
            if (dt.Rows.Count == 0)
            {
                sql = "insert into SystemRightSet(RightIDs) values ('')";
                db.RunSql(sql);
                return "";
            }
            else
            {
                return dt.Rows[0][0].ToString();
            }
        }

        internal static void SaveRightSet(string rightIDs)
        {
            string sql = "update SystemRightSet set RightIDs = '" + rightIDs + "'";
            DBHelper.Instance().RunSql(sql);
        }
    }
}
